<?php
class Areas extends Controller{

	function areas(){
		parent::Controller();
		$this->load->helper('url');
		$this->load->helper('form');
		$this->load->library(array('form_validation','session'));
    
		$this->load->model('area_model');
    $user_ses = $this->session->userdata('userlogindetails');
 
 
	}
//to display area names...
	function list_areas()
	{
	  $user_ses = $this->session->userdata('userlogindetails');
    
		if($user_ses['user_id'] && $user_ses['user_group']==1)
    {
    
    		$displayform = '';
    		$displayform .='<div id=dashboard ><span>Manage Area</span> <a href='.base_url().'/idea_home/admin_dashboard ><img align=right src="'.base_url().'images/home.png" alt="home" /></a><img align=right src="'.base_url().'images/back.png" alt="back" id=back ONCLICK="history.go(-1)" /></div>';
        	$displayform .= '<span style="float:left;">Welcome '.$user_ses['user_name'].'</span><div id=logout><a href='.base_url().'areas/add_area_in_bulk >Upload Areas</a>&nbsp;<a href='.base_url().'areas/new_area >Add New Area</a>&nbsp;<a href="'.base_url().'idea_home/logout">Logout</a></div>';
        	$displayform .= '<br/>'; 
    		$displayform .='<div align=right></div>';
    		$displayform .= '<form id=show_areas >';
    		
    		$displayform .= '<table>';
    		$displayform .= '<tr><td align=left>State:</td>';
    		$displayform .= '<td align=left><select name=state id=state class=required >';
    		$displayform .= '<option value="">-Please Select State-</option>';
    		$this->load->model('states_model');
    		$states = $this->states_model->disp_states();
    		foreach($states->result_array() as $row)
    		{
    			$displayform .= '<option value="'.$row['state_id'].'">'.$row['state_name'].'</option>';
    		}
    		$displayform .= '</select></td>';
    		$displayform .= '<td>';
    		$displayform .= 'City:</td><td id=city_select>';
    		$displayform .= '<select name=cities id=city class=required>';
    		$displayform .= '<option value="">-Please select city-</option>';
    		$displayform .= '</select>';
    		$displayform .= '</td>';
    		$displayform .= '<td ><input type=submit name=areas id=area value="Show Areas" /></td></tr>';
    		$displayform .= '</table>';
    		
    		$displayform .= '</form>';
    		
    	
    		$displayform .= '<div id="display_areas"></div>';
    		
    		$this->form_validation->set_rules('cities', 'City Name', 'trim|required|xss_clean');
    		$this->form_validation->set_rules('state', 'State Name', 'trim|required|xss_clean');
    		$data = array('formdisplay'=>$displayform );
    		
    		$this->template->add_js('	
    			$(document).ready(function(){
    			$("#state").change(function(){
    				var src = $(this).val();
    
    			 	$.ajax({
    			type: "GET",
    			url: "/areas/display_cities/"+ src,
    
    			dataType: "text/html",
    			success: function(html){       $("#city_select").html(html);     }
    
    			  });
    
    			});
    			});
    			','embed');
    			
    		$this->template->add_js('	
    			$(document).ready(function(){
    			$("#area").click(function(){
    				var src = $("#city").val();
    			if(src == "")
    			{
    				alert("please select state and city to display area details!!");
    				return false;
    			}
    			 	$.ajax({
    			type: "GET",
    			url: "/areas/fetch_areas/"+ src,
    
    			dataType: "text/html",
    			success: function(html){       $("#display_areas").html(html);     }
    
    			  });
    			return false;
    			});
    			});
    			','embed');
          $this->template->add_css('css/flexigrid/flexigrid.css');
          
    			$this->template->write_view('header','header', $data, True);
    			$this->template->write_view('content', 'general');
    			$this->template->render();
    	}
    else
  {
    echo "forbidden to access!!";
  }
	}
	
  	function add_area_in_bulk()
	{
		$user_ses = $this->session->userdata('userlogindetails');
    
		if($user_ses['user_id'] && $user_ses['user_group']==1)
    	{
			$this->load->model('states_model');
      		$states = $this->states_model->disp_states();
			
    		$displayform ='';
			$displayform .='<div id=dashboard ><span>Upload Cities</span> <a href='.base_url().'/idea_home/admin_dashboard ><img align=right src="'.base_url().'images/home.png" alt="home" /></a><img align=right src="'.base_url().'images/back.png" id="back"" alt="back" ONCLICK="history.go(-1)" /></div>';
			$displayform .= '<span style="float:left;">Welcome '.$user_ses['user_name'].'</span><div id=logout><a href="'.base_url().'idea_home/logout">Logout</a></div>';
			$displayform .= '<div id=upload>';
			$attr = array('class'=>'add_outward','id'=>'add_upload');
			$displayform .= form_open_multipart('areas/save_bulk_areas',$attr);
			$displayform .= '<fieldset id="upload_caf" style="width:40%;">';
	    	$displayform .= ' <legend style="color:black;">Upload File</legend>';
	    	$displayform .= '<center><table cellspacing="20px" >';
			$displayform .= '<tr><td align=right>State:</td>';
		$displayform .= '<td align=left><select name=state id=state class=required >';
		$displayform .= '<option value="">-Please Select State-</option>';
		$this->load->model('states_model');
		$states = $this->states_model->disp_states();
		foreach($states->result_array() as $row)
		{
			$displayform .= '<option value="'.$row['state_id'].'">'.$row['state_name'].'</option>';
		}
		$displayform .= '</select></td></tr>';
		$displayform .= '<tr ><td align=right>';
		$displayform .= 'City:</td><td id=city_select>';
		$displayform .= '<select name=cities class=required>';
		$displayform .= '<option value="">-Please select city-</option>';
		$displayform .= '</select>';
		$displayform .= '</td></tr>';
			
	    	$displayform .= '<tr><td align=left><img src="'.base_url().'images/upload.png" alt=upload /></td>';
			
			$displayform .= '<td align=left><input type="file" name="userfile" size="20" class="required" accept="xls" />';
			$displayform .= '</td></tr>';
			$displayform .='<tr><td colspan=2 align=center><input class="submit" type="submit" value="Save" />';
			$displayform .='<input type="reset" value="Cancel" /></td>';
			$displayform .= '</tr></table>';
			
			$displayform .= '</fieldset>';
			$displayform .= '</form>';
			
			
			$displayform .= '</div>';
			$data = array('formdisplay'=>$displayform);
			$this->template->add_js('js/jquery-latest.js');	
			$this->template->add_js('js/jquery.validate.js');
			$this->template->add_js('js/jquery.ui.progressbar.js');
			$this->template->add_js('	
			$(document).ready(function(){
			$("#state").change(function(){
				var src = $(this).val();

			 	$.ajax({
			type: "GET",
			url: "/areas/display_cities/"+ src,

			dataType: "text/html",
			success: function(html){       $("#city_select").html(html);     }

			  });

			});
			});
			','embed');
			
			$this->template->add_js('$(document).ready(function(){
		 		   $("#add_upload").validate();
		  			});
			','embed');
			
	
				$this->template->write_view('header','header', $data, True);
				$this->template->write_view('content', 'other_general');
				$this->template->render();
		}
		   else
		  {
		    echo "forbidden to access!!";
		  }
	}
	function save_bulk_areas()
	{
		$dir = './areas_uploads/';


		$config['upload_path'] = $dir;
		$config['allowed_types'] = 'xls';
		$config['max_size']	= '2097152';
		$config['max_width']  = '1024';
		$config['max_height']  = '768';
		$this->load->library('upload',$config);
				
		if (!$this->upload->do_upload()) 
				{
					$error = array('error' => $this->upload->display_errors());
					print_r($error);
				}    
				else
				{	
					$filename = array('upload_data' => $this->upload->data());
					
					$this->load->model('area_model');
					$filepath = $filename['upload_data']['file_name'];
				
					$this->area_model->insert_areas_bulk($filepath);
					redirect('areas/add_area_in_bulk');
				   //	redirect(base_url().'cities/add_cities_in_bulk');					
				}
	}
  
	function fetch_areas($id=0)
	{
			$user_ses = $this->session->userdata('userlogindetails');
    
    if($user_ses['user_id'] && $user_ses['user_group']==1)
    {
			
			$this->load->helper('flexigrid');
            
      $displayform ='';
      $city = $this->area_model->get_cityname($id);
      
      $city_name = $city->result_array();
         
      $displayform .= '<table id="flex1" style="display:none" border=0 align=left ></table>';
      $displayform .= '<br/>';
    
      $displayform .='<div><script language="javascript" src="'.base_url().'js/flexigride/flexigrid.pack.js" ></script></div>';
        
    $colModel['area_id'] = array('Sl No',60,TRUE,'center',0);
    $colModel['area_name'] = array('Area Name',160,TRUE,'left',2);
    $colModel['area_pincode'] = array('Area Pincode',160,TRUE,'left',2);
    
    $gridParams = array(
      'width' => '800',
      'height' => 400,
      'rp' => 15,
      'rpOptions' => '[10,20,30,40]',
      'pagestat' => 'Displaying:{from} to {to} of {total} items.',
      'blockOpacity' => 0.5,
      'title' => 'List of Areas in '.$city_name[0]['city_name'].'',
      'showTableToggleBtn' => true
    );
    
    $grid_js = build_grid_js('flex1',site_url("/ajax/areas_list/".$id),$colModel,'caf_id','asc',$gridParams);
    
    echo $displayform;
    echo $grid_js;
    }
    else
    {
      echo "Forbidden to access!!";
    } 
 	}


//to save edited information	
	function area_edit($id=0)
	{
	  $user_ses = $this->session->userdata('userlogindetails');
    
    if($user_ses['user_id'] && $user_ses['user_group']==1)
    {
      
		$this->load->helper('form');
		$this->load->library('form_validation');
		$displayform ='';

			$area_name = $this->area_model->area_edit($id);
			$name = $area_name->result_array();
			$area_name = array('name'=>'area_name','value'=>$name[0]['area_name'],'type'=>'text','class'=>'required');//its already posted so it will not set again here after validati
			$pincode = array('name'=>'pincode','value'=>$name[0]['area_pincode'],'type'=>'text','class'=>'required');//its already posted so it will not set again here after validati
		
		//$this->form_validation->set_rules('state_name', 'State Name', 'trim|required|xss_clean');
		$displayform ='';
		$displayform .='<div id=dashboard ><span>Manage Area</span> <a href='.base_url().'/idea_home/admin_dashboard ><img align=right src="'.base_url().'images/home.png" alt="home" /></a><img align=right src="'.base_url().'images/back.png" alt="back" id=back ONCLICK="history.go(-1)" /></div>';
	  $displayform .= '<span style="float:left;">Welcome '.$user_ses['user_name'].'</span><div id=logout><a href="'.base_url().'idea_home/logout">Logout</a></div>';
    $displayform .= '<br/>';
    
		$displayform .='<fieldset style="width:50%;"><legend>Edit Area Name</legend>';
		$attr = array('id'=>'edit_area');
		$displayform .= form_open('areas/save_edit',$attr);
		$displayform .= '<input type=hidden name=id value='.$id.' />';
		$displayform .= '<table class="tablestyle">';
		$displayform .= '<tr><td align=right> Area Name:</td>';
		$displayform .= '<td>'.form_input($area_name).'</td>';
		$displayform .= '</tr>';
		$displayform .= '<tr><td align=right> Pincode:</td>';
    $displayform .= '<td>'.form_input($pincode).'</td>';
    $displayform .= '</tr>';
    
		$displayform .= '<tr><td align=right colspan=2>'.form_submit('submit','Update area').'</td>';
		$displayform .= '</tr>';
		$displayform .= '</table></fieldset>';
		$displayform .=form_close();
		
		//$displayform .= '<div align="right"><input type=button value="Go Back" ONCLICK="history.go(-1)" /></div>';
		
			$data = array('formdisplay'=>$displayform );
			$this->template->add_js('js/jquery-latest.js');	
			$this->template->add_js('js/jquery.validate.js');
			
			$this->template->add_js('$(document).ready(function(){
	 		   $("#edit_area").validate();
	  			});
		','embed');
			$this->template->write_view('header','header', $data, True);
			$this->template->write_view('content', 'general');
			$this->template->render();
		}
else
  {
    echo "Forbidden to access!!";
  }  
		}
		
		//TO SAVE EDITED INFORMATION.........
		function save_edit()
		{
		//print_r($_POST);
			$user_ses = $this->session->userdata('userlogindetails');
    
    if($user_ses['user_id'] && $user_ses['user_group']==1)
    {
			$this->area_model->save_edit();
			redirect('areas/list_areas');
			}
    else
      {
        echo "forbidden to access";
      }	
		}
		
		
	
	
	function new_area()
	{
		$user_ses = $this->session->userdata('userlogindetails');
    
    if($user_ses['user_id'] && $user_ses['user_group']==1)
    {
    		
		
		$displayform = '';
		$displayform .='<div id=dashboard ><span>Manage Area</span> <a href='.base_url().'/idea_home/admin_dashboard ><img align=right src="'.base_url().'images/home.png" alt="home" /></a><img align=right src="'.base_url().'images/back.png" alt="back" id=back ONCLICK="history.go(-1)" /></div>';
		$area_name = array('name'=>'area_name','id'=>'area_name',
				'type'=>'text','class'=>'required');
    $pincode = array('name'=>'pincode','id'=>'pincode',
        'type'=>'text','class'=>'required number');
		$attr = array('id'=>'new_area');
		$displayform .='<fieldset style="width:50%;"><legend>Add New Area</legend>';
		$displayform .= form_open('areas/new_area',$attr);
		$displayform .= '<table>';
		$displayform .= '<tr><td align=right>State:</td>';
		$displayform .= '<td align=left><select name=state id=state class=required >';
		$displayform .= '<option value="">-Please Select State-</option>';
		$this->load->model('states_model');
		$states = $this->states_model->disp_states();
		foreach($states->result_array() as $row)
		{
			$displayform .= '<option value="'.$row['state_id'].'">'.$row['state_name'].'</option>';
		}
		$displayform .= '</select></td></tr>';
		$displayform .= '<tr ><td align=right>';
		$displayform .= 'City:</td><td id=city_select>';
		$displayform .= '<select name=cities class=required>';
		$displayform .= '<option value="">-Please select city-</option>';
		$displayform .= '</select>';
		$displayform .= '</td></tr>';
		$displayform .= '<tr>';
		$displayform .= '<td align=right>Area:</td>';
		$displayform .= '<td align=left>'.form_input($area_name).'</td></tr>';
    $displayform .= '<tr>';
    $displayform .= '<td align=right >Pincode:</td>';
    $displayform .= '<td align=left>'.form_input($pincode).'</td></tr>';
		$displayform .= '<tr><td colspan=2 align=right >'.form_submit('submit','Save New Area').'</td></tr>';
		
		$displayform .= '</table>';
		$displayform .= form_close();
		$displayform .='</fieldset>';
		//$displayform .= '<div align="right"><input type=button value="Go Back" ONCLICK="history.go(-1)" /></div>';
		
		$this->form_validation->set_rules('state', 'State Name', 'trim|required|xss_clean');
		$this->form_validation->set_rules('cities', 'City Name', 'trim|required|xss_clean');
		$this->form_validation->set_rules('area_name', 'Area Name', 'trim|required|xss_clean');
    $this->form_validation->set_rules('pincode', 'Area Pincode', 'trim|required|xss_clean');
		$data = array('formdisplay'=>$displayform );
		
		$this->template->add_js('	
			$(document).ready(function(){
			$("#state").change(function(){
				var src = $(this).val();

			 	$.ajax({
			type: "GET",
			url: "/areas/display_cities/"+ src,

			dataType: "text/html",
			success: function(html){       $("#city_select").html(html);     }

			  });

			});
			});
			','embed');
			$this->template->add_js('js/jquery-latest.js');	
			$this->template->add_js('js/jquery.validate.js');
		
			$this->template->add_js('$(document).ready(function(){
	 		   $("#new_area").validate();
	  			});
		','embed');
		if ($this->form_validation->run() == FALSE)
		{
			$this->template->write_view('header','header', $data, True);
			$this->template->write_view('content', 'general');
			$this->template->render();
		}
		else
		{
			$this->load->model('area_model');
			$this->area_model->insert_area();
			redirect('areas/list_areas');
		}
		
		}
  else
  {
    echo "forbidden to access";
  } 
	}
	
//to for ajax...	
	function display_cities($id=0)
	{
	   $user_ses = $this->session->userdata('userlogindetails');
    
    if($user_ses['user_id'] && $user_ses['user_group']==1)
    {	
		
    		$this->load->model('cities_model');
    		$states = $this->cities_model->disp_cities($id);
    		$ajaxVal = '';
    		$ajaxVal .= '<select name=cities id=city class=required>';
    		$ajaxVal .= '<option value="" >-Please select city-</option>';
    		foreach ($states->result_array() as $row)
    		{
    			$ajaxVal .= '<option value="'.$row['city_id'].'">'.$row['city_name'].'</option>';
    		}
    		$ajaxVal .='</select>';
    		
    		echo $ajaxVal;
        
    }
    else
    {
      echo "Forbidden to access!";
    }
	}
	
//to display duplicate area message...	
	function duplicate_area()
	{
	  $user_ses = $this->session->userdata('userlogindetails');
    
    if($user_ses['user_id'] && $user_ses['user_group']==1)
    {
        		$displayform ='';
        		$displayform .='<div id=dashboard ><span>Manage Area</span> <a href='.base_url().'/idea_home/admin_dashboard ><img align=right src="'.base_url().'images/home.png" alt="home" /></a><img align=right src="'.base_url().'images/back.png" alt="back" id=back ONCLICK="history.go(-1)" /></div>';
        		$displayform .= "This place already exist,please go back and try for another name!!";
        		//$displayform .= '<div align="right"><input type=button value="Go Back" ONCLICK="history.go(-1)" /></div>';
        		$data = array('formdisplay'=>$displayform );
        		$this->template->write_view('header','header', $data, True);
        			$this->template->write_view('content', 'other_general');
        			$this->template->render();
    }
else
    {
      echo "Forbidden to access!";
    }
	}
	

        
	
}
